// In Class API Program Entry Point

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include <iostream>
#include <cstdlib>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif

using namespace std;

#include "Game.h"

Game::Game() 
{
   orthoView = false;
   scene = new Scene;
   camera = new Camera(scene->getCueBall());
   collisionDetector = new CollisionDetector(scene);
}

Game::~Game() 
{
   if (camera) delete camera;
   if (scene) delete scene;
   if (collisionDetector) delete collisionDetector;
}

void Game::draw() 
{
   glClearColor(0.0,0.0,0.0,1.0);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   
   collisionDetector->check();
   
   bool movingBalls = collisionDetector->movingBalls();
   
   camera->position(orthoView, movingBalls);
   scene->draw(orthoView, movingBalls);
   
   glutSwapBuffers();  
}

void Game::reshape(int w, int h)
{
   camera->reshape(w, h, orthoView);
   width = w;
   height = h;
}

Camera* Game::getCamera()
{
   return camera;
}

Scene* Game::getScene()
{
   return scene;
}

int Game::getWidth()
{
   return width;
}

int Game::getHeight()
{
   return height;
}

void Game::setTopView(bool _topView)
{
   orthoView = _topView;
   camera->reshape(width, height, orthoView);
}

bool Game::isTopView()
{
   return orthoView;
}
